#include <ptgui/ptgui.h>
#include <ptgui/color.h>

/*
 * RTGUI default color format
 * BBBB BBBB GGGG GGGG RRRR RRRR
 */

/* convert ptgui color to BBBBBGGGGGGRRRRR */
rt_uint16_t ptgui_color_to_565(ptgui_color_t c)
{
	rt_uint16_t pixel;

	pixel = (rt_uint16_t)(((PTGUI_RGB_B(c) >> 3) << 11) | ((PTGUI_RGB_G(c) >> 2) << 5) | (PTGUI_RGB_R(c) >> 3));

	return pixel;
}

ptgui_color_t ptgui_color_from_565(rt_uint16_t pixel)
{
	rt_uint16_t r,
				g,
				b;
	ptgui_color_t color;

	r = pixel & 0x1f;
	g = (pixel >> 5) & 0x3f;
	b = (pixel >> 11) & 0x1f;

	color = r * 8225 / 1024 + ((g * 4047 / 1024) << 8) + ((b * 8225 / 1024) << 16);

	return color;
}

/* convert ptgui color to RRRRRGGGGGGBBBBB */
rt_uint16_t ptgui_color_to_565p(ptgui_color_t c)
{
	rt_uint16_t pixel;

	pixel = (rt_uint16_t)(((PTGUI_RGB_R(c) >> 3) << 11) | ((PTGUI_RGB_G(c) >> 2) << 5) | (PTGUI_RGB_B(c) >> 3));
	return pixel;
}

ptgui_color_t ptgui_color_from_565p(rt_uint16_t pixel)
{
	rt_uint8_t r,
			   g,
			   b;
	ptgui_color_t color;

	r = (pixel >> 11) & 0x1f;
	g = (pixel >> 5) & 0x3f;
	b = pixel & 0x1f;

	color = r * 8225 / 1024 + ((g * 4047 / 1024) << 8) + ((b * 8225 / 1024) << 16);

	return color;
}